import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by dllo on 18/5/2.
 */
//批量的修改和插入  最好放在事务中
public class Test03 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        long starttime= System.currentTimeMillis();
        insertWithTrans();
        long endTime = System.currentTimeMillis();
        System.out.println("总耗时:"+(endTime-starttime)+"ms");
    }
    public static Connection getConn(){
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lanou3",
                    "root","123456");
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void insertWithTrans(){
        Connection conn = getConn();
        String sql= "INSERT INTO BANK (cname,money) " + "VALUES (?,100)";
        try {
            // 开启事务
            conn.setAutoCommit(false);
            for (int i = 0; i < 10000; i++) {
                System.out.println("这是第:" + i + "次插入");
                PreparedStatement statement= conn.prepareStatement(sql);
                statement.setString(1,"NAME-"+i);
                statement.executeUpdate();
                statement.close();
            }
            conn.commit();//提交事务
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static void insertNormal(){
        Connection conn =getConn();
        String sql= "INSERT INTO BANK (cname,money) " + "VALUES (?,100)";
        for (int i = 0; i < 10000; i++) {
            System.out.println("这是第:" + i + "次插入");
            try {
                PreparedStatement statement=conn.prepareStatement(sql);
                statement.setString(1,"name-"+i);
                statement.executeUpdate();//执行
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        }
        //一定要放在for结束之后
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
